package net.zjitc.servlet;


import net.zjitc.domain.Book;
import net.zjitc.service.BookService;
import net.zjitc.service.BookServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/10.
 */
@WebServlet(name = "PurchaseServlet",urlPatterns = "/purchaseServlet")
public class PurchaseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id=request.getParameter("id");
        BookService bookService=new BookServiceImpl();
        Book book=bookService.findById(id);
        HttpSession session=request.getSession(true);
        List<Book> cart=null;
        cart= (List<Book>) session.getAttribute("cart");
        if(cart==null){
            cart=new ArrayList<>();
            session.setAttribute("cart",cart);
        }
        cart.add(book);
        response.sendRedirect("/Web/cartServlet");
    }
}
